import 'package:flutter/material.dart';

enum NotificationType {
  like,       // 点赞
  comment,    // 评论
  follow,     // 关注
  system,     // 系统通知
  message     // 私信
}

class Notification {
  final String id;
  final String userId;           // 发送者ID
  final String targetUserId;     // 接收者ID
  final String content;          // 通知内容
  final String? imageUrl;        // 相关图片
  final String? relatedId;       // 相关内容ID（帖子、评论、宠物等）
  final String? relatedType;     // 相关内容类型（"post", "pet", "video"等）
  final NotificationType type;   // 通知类型
  final DateTime createdAt;      // 创建时间
  final bool isRead;             // 是否已读

  Notification({
    required this.id,
    required this.userId,
    required this.targetUserId,
    required this.content,
    this.imageUrl,
    this.relatedId,
    this.relatedType,
    required this.type,
    required this.createdAt,
    this.isRead = false,
  });

  // 从JSON转换为Notification对象
  factory Notification.fromJson(Map<String, dynamic> json) {
    return Notification(
      id: json['id'],
      userId: json['userId'],
      targetUserId: json['targetUserId'],
      content: json['content'],
      imageUrl: json['imageUrl'],
      relatedId: json['relatedId'],
      relatedType: json['relatedType'],
      type: NotificationType.values.firstWhere(
        (e) => e.toString().split('.').last == json['type'],
        orElse: () => NotificationType.system,
      ),
      createdAt: DateTime.parse(json['createdAt']),
      isRead: json['isRead'] ?? false,
    );
  }

  // 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'targetUserId': targetUserId,
      'content': content,
      'imageUrl': imageUrl,
      'relatedId': relatedId,
      'relatedType': relatedType,
      'type': type.toString().split('.').last,
      'createdAt': createdAt.toIso8601String(),
      'isRead': isRead,
    };
  }

  // 获取通知图标
  IconData getNotificationIcon() {
    switch (type) {
      case NotificationType.like:
        return Icons.favorite;
      case NotificationType.comment:
        return Icons.comment;
      case NotificationType.follow:
        return Icons.person_add;
      case NotificationType.system:
        return Icons.notifications;
      case NotificationType.message:
        return Icons.message;
    }
  }

  // 获取通知图标颜色
  Color getNotificationColor() {
    switch (type) {
      case NotificationType.like:
        return Colors.red;
      case NotificationType.comment:
        return Colors.blue;
      case NotificationType.follow:
        return Colors.green;
      case NotificationType.system:
        return Colors.orange;
      case NotificationType.message:
        return Colors.purple;
    }
  }

  // 创建已读版本
  Notification markAsRead() {
    return Notification(
      id: id,
      userId: userId,
      targetUserId: targetUserId,
      content: content,
      imageUrl: imageUrl,
      relatedId: relatedId,
      relatedType: relatedType,
      type: type,
      createdAt: createdAt,
      isRead: true,
    );
  }
} 